import base from '../api/base.js'
const getHeader = () => {
    const userInfo = uni.getStorageSync('USER_INFO')
    return userInfo ? {
        'Auth-id': JSON.parse(userInfo).authId,
        'Auth-token': JSON.parse(userInfo).authToken,
    } : {}
}
export const request = (url, data, methods = true) => {
	console.log(data)
    const baseUrl = `${base.url}${url}`
    const promise = new Promise((resolve, reject) => {
        uni.request({
            url: baseUrl,
            data,
            header: getHeader(),
            method: methods ? 'GET' : 'POST',
            success(res) {
                const { data, code, message } = res.data
                switch (code) {
                    case '200':
                        resolve(data)
                        break;
                    case '1002':
                        uni.showToast({
                            title: '登录失效，请重新登录',
                            duration: 1500,
                            icon: 'none',
                            success() {
                                reLogin()
                            }
                        })
                        break;
                    default:
                        reject(data);
                        uni.showToast({
                            title: message,
                            duration: 2000,
                            icon: 'none'
                        })
                        break;
                }
            }
        })
    })
    return promise
}
const reLogin = () => {
    const route = getCurrentPages()
    console.log(route)
    uni.redirectTo({
        url: '/pages/login-launch/index'
    })
    uni.removeStorageSync('USER_INFO')
}